package com.excellent.system.controller;

import com.excellent.common.annotation.Log;
import com.excellent.common.config.ExcellentConfig;
import com.excellent.common.core.controller.BaseController;
import com.excellent.common.core.domain.AjaxResult;
import com.excellent.common.core.page.TableDataInfo;
import com.excellent.common.enums.BusinessType;
import com.excellent.common.utils.poi.ExcelUtil;
import com.excellent.system.domain.SysTaskStu;
import com.excellent.system.service.ISysRoleService;
import com.excellent.system.service.ISysTaskStuService;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

/**
 * 学生课题Controller
 *
 * @author excellent
 * @date 2021-06-20
 */
@Controller
@RequestMapping("/system/admintask")
public class SysTaskStuAdminController extends BaseController
{
    private String prefix = "system/admintask";

    @Autowired
    private ISysTaskStuService sysTaskStuService;

    @Autowired
    private ISysRoleService roleService;

    @GetMapping()
    public String admintask()
    {
        return prefix + "/admintask";
    }

    /**
     * 查询学生课题列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysTaskStu sysTaskStu)
    {
        if (sysTaskStu.getTaskTerm().equals("")) {
            Calendar c = Calendar.getInstance();  //获取系统当前年份
            int month = c.get(Calendar.MONTH) + 1;//本函数中月份需要加一
            int year = c.get(Calendar.YEAR);
            if (month >= 1 && month <= 6) {
                sysTaskStu.setTaskTerm((year-1)+"-"+(year) + "学年春季学期");
            } else {
                sysTaskStu.setTaskTerm((year)+"-"+(year + 1) + "学年秋季学期");
            }
            startPage();
            List<SysTaskStu> list = sysTaskStuService.selectSysTaskStuList(sysTaskStu);
            Collections.sort(list);
            return getDataTable(list);
        } else {
            startPage();
            List<SysTaskStu> list = sysTaskStuService.selectSysTaskStuList(sysTaskStu);
            Collections.sort(list);
            return getDataTable(list);
        }
    }

    /**
     * 导出学生课题列表
     */
    @Log(title = "学生课题", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysTaskStu sysTaskStu) throws IOException {
        List<SysTaskStu> list = sysTaskStuService.selectSysTaskStuList(sysTaskStu);
        //创建Excel表对象
        XSSFWorkbook wb = new XSSFWorkbook();
        //创建表名
        XSSFSheet sheet = wb.createSheet("task");
        XSSFRow row;
        XSSFCell cell;
        XSSFFont font = wb.createFont();
        font.setFontHeightInPoints((short)15);
        XSSFCellStyle cellStyle = wb.createCellStyle();  //样式一
        cellStyle.setWrapText(true);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        XSSFCellStyle cellStyle1 = wb.createCellStyle();  //样式二
        cellStyle1.setWrapText(true);
        cellStyle1.setAlignment(HorizontalAlignment.CENTER);
        cellStyle1.setFont(font);
        row = sheet.createRow(0);  //创建第一行
        cell = row.createCell(0);
        row = sheet.createRow(1);  //创建第二行
        row.setHeight((short) 600);  //设置行高
        cell = row.createCell(0);
        String s = list.get(0).getStuId().toString();  //获取多少年级
        String s1 = s.substring(0,4);
        Calendar c = Calendar.getInstance();  //获取系统当前年份
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH) + 1;//本函数中月份需要加一
        int count = 0; //学期数
        String s2 = null;
        int c1; //学年情况
        int c2; //学年情况
        if(month>0 && month <= 6){
            c1 = year-1;
            c2 = year;
            count = 2;
            s2 = c1+"-" + c2;
        }else{
            c1 = year;
            c2 = year+1;
            count = 1;
            s2 = c1+"-" + c2;
        }
        cell.setCellValue("软件学院 " + s1 + "级 " + s2 + " 学年 第 "  + count +  " 学期");
        cell.setCellStyle(cellStyle1);
        row = sheet.createRow(2);  //创建第三行
        row.setHeight((short) 600);
        cell = row.createCell(0);
        cell.setCellValue("实践环节 成绩表");
        cell.setCellStyle(cellStyle1);
        row = sheet.createRow(3);  //创建第四行
        row.setHeight((short) 600);
        cell = row.createCell(0);
        String task = null;
        switch (list.get(0).getTaskClass()){
            case "RB一级实践环节": task = "RB7001039";
                break;
            case "RB二级实践环节": task = "RB7001038";
                break;
            case "RB三级实践环节": task = "RB7001040";
                break;
            case "IT行业实践一": task = "RB7001042 ";
                break;
            case "IT行业实践二": task = "RB7001043";
                break;
            case "软件工程实践一": task = "RB7001045";
                break;
            case "软件工程实践二": task = "RB7001046";
                break;
            case "实践成果一": task = "RB7001226";
                break;
            case "实践成果二": task = "RB7001227";
                break;
        }
        cell.setCellValue("课程代号： " +task + "                     课程名称: " + list.get(0).getTaskClass());
        cell.setCellStyle(cellStyle);
        row = sheet.createRow(4);  //创建第五行
        String[] data = {"序号","学号","姓名","班级","课题名称","成绩","指导教师"};
        for (int i = 0; i < 7; i++) {
            cell = row.createCell(i);
            cell.setCellValue(data[i]);
            cell.setCellStyle(cellStyle);
        }
        for (int i = 0; i <list.size() ; i++) {
            row = sheet.createRow(5+i);
            cell = row.createCell(0);
            cell.setCellValue(i+1);
            cell.setCellStyle(cellStyle);
            cell = row.createCell(1);
            cell.setCellValue(String.valueOf(list.get(i).getStuId()));
            cell.setCellStyle(cellStyle);
            cell = row.createCell(2);
            cell.setCellValue(String.valueOf(list.get(i).getStuName()));
            cell.setCellStyle(cellStyle);
            cell = row.createCell(3);
            cell.setCellValue(String.valueOf(list.get(i).getStuClass()));
            cell.setCellStyle(cellStyle);
            cell = row.createCell(4);
            cell.setCellValue(String.valueOf(list.get(i).getTaskName()));
            cell.setCellStyle(cellStyle);
            cell = row.createCell(5);
            cell.setCellValue(String.valueOf(list.get(i).getScore()));
            cell.setCellStyle(cellStyle);
            cell = row.createCell(6);
            cell.setCellValue(String.valueOf(list.get(i).getTeaName()));
            cell.setCellStyle(cellStyle);
        }
        row = sheet.createRow(6+list.size());  //创建倒数第二行
        cell = row.createCell(0);
        cell.setCellValue("任课教师：                        教研室主任：");
        row = sheet.createRow(7+list.size());  //创建倒数第一行
        cell = row.createCell(0);
        cell.setCellValue("                                                        年  月  日");
        sheet.setColumnWidth(1, 4500); //设置列宽
        sheet.setColumnWidth(3, 6000); //设置列宽
        sheet.setColumnWidth(4, 7000); //设置列宽
        CellRangeAddress region = new CellRangeAddress(0, 0, 0, 6);
        CellRangeAddress region1 = new CellRangeAddress(1, 1, 0, 6);
        CellRangeAddress region2 = new CellRangeAddress(2, 2, 0, 6);
        CellRangeAddress region3 = new CellRangeAddress(3, 3, 0, 6);
        CellRangeAddress region4 = new CellRangeAddress(6+list.size(), 6+list.size(), 0, 6);
        CellRangeAddress region5 = new CellRangeAddress(7+list.size(), 7+list.size(), 0, 6);
        sheet.addMergedRegion(region);
        sheet.addMergedRegion(region1);
        sheet.addMergedRegion(region2);
        sheet.addMergedRegion(region3);
        sheet.addMergedRegion(region4);
        sheet.addMergedRegion(region5);
        String filename = "";
        filename = UUID.randomUUID().toString() + "_" + "admintask.xlsx";
        String downloadPath = ExcellentConfig.getDownloadPath() + filename;
        File desc = new File(downloadPath);
        desc.getParentFile().mkdirs();
        wb.write(new FileOutputStream(downloadPath));
        return AjaxResult.success(filename);
    }

    @Log(title = "课题成绩", businessType = BusinessType.EXPORT)
    @PostMapping("/export1")
    @ResponseBody
    public AjaxResult export1(SysTaskStu sysTaskStu) {
        Calendar c = Calendar.getInstance();  //获取系统当前年份
        int month = c.get(Calendar.MONTH) + 1;//本函数中月份需要加一
        int year = c.get(Calendar.YEAR);
        if (month >= 1 && month <= 6) {
            sysTaskStu.setTaskTerm((year - 1) + "学年第2学期");
        } else {
            sysTaskStu.setTaskTerm(year + "学年第1学期");
        }
        sysTaskStu.setScore(0);
        List<SysTaskStu> list = sysTaskStuService.selectSysTaskStuList(sysTaskStu);
        Collections.sort(list);
        ExcelUtil<SysTaskStu> util = new ExcelUtil<SysTaskStu>(SysTaskStu.class);
        return util.exportExcel(list, "admintask");
    }
    /**
     * 新增学生课题
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存学生课题
     */
    @Log(title = "学生课题", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysTaskStu sysTaskStu)
    {
        return toAjax(sysTaskStuService.insertSysTaskStu(sysTaskStu));
    }

    /**
     * 修改学生课题
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysTaskStu sysTaskStu = sysTaskStuService.selectSysTaskStuById(id);
        mmap.put("sysTaskStu", sysTaskStu);
        return prefix + "/edit";
    }

    /**
     * 修改保存学生课题
     */
    @Log(title = "学生课题", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysTaskStu sysTaskStu)
    {
        return toAjax(sysTaskStuService.updateSysTaskStu(sysTaskStu));
    }

    /**
     * 删除学生课题
     */
    @Log(title = "学生课题", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysTaskStuService.deleteSysTaskStuByIds(ids));
    }
}
